package com.example.netdemo;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;

import com.example.netdemo.databinding.DialogLoadingBinding;


public class LoadingDialog extends Dialog {

    private Context context;
    private String mLoadingTips;
    private boolean mCanceledOnTouchOutside;
    private DialogLoadingBinding binding;

    public LoadingDialog(Context context) {
        this(context, null);
    }

    public LoadingDialog(Context context, String tips, boolean canceledOnTouchOutside) {
        super(context, R.style.CommonDialog);
        this.context = context;
        this.mLoadingTips = tips;
        this.mCanceledOnTouchOutside = canceledOnTouchOutside;
    }

    public LoadingDialog(Context context, String tips) {
        this(context, tips, false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogLoadingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
        setCancelable(true);
        //清除背景模糊及阴影效果
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.setCanceledOnTouchOutside(mCanceledOnTouchOutside);
    }

    private void init() {
        if (mCanceledOnTouchOutside) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
        }
    }

    public void dismissDialog() {
        dismiss();
    }
}
